package com.example.goodpet.my.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.UserInfoActivity;
import com.example.goodpet.bean.Follow;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.User;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.sql.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyFollowAdapter extends RecyclerView.Adapter<MyFollowAdapter.ViewHolder>{

    private List<User> lists;
    private Context context;
    private long userId;
    private UserService userService;
    public MyFollowAdapter(List<User> lists, Context context,long userId) {
        this.lists = lists;
        this.context = context;
        userService= RetrofitSingleton.getRetrofitNetConfig().create(UserService.class);
        this.userId=userId;
    }

    @NonNull
    @Override
    public MyFollowAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_follow, parent, false);
        return new ViewHolder(inflate);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public void onBindViewHolder(@NonNull MyFollowAdapter.ViewHolder holder, int position) {
        User user = lists.get(position);
        Glide.with(context).load(Constants.BASE_URL_IMAGE+user.getPortrait()).into(holder.portrait);
        if (user.getGender()==0)holder.ivSex.setImageDrawable(context.getDrawable(R.drawable.man));
        else if (user.getGender()==1)holder.ivSex.setImageDrawable(context.getDrawable(R.drawable.women));
        holder.tvNickname.setText(user.getNickname());
        holder.tvSignature.setText(user.getSignature());
        if (MyApplication.user.getId()!=userId){holder.btnFollow.setVisibility(View.INVISIBLE);}


        initListener(holder,position);
    }

    private void initListener(ViewHolder holder,int position) {
        User user = lists.get(position);
        holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserPetInfo userPetInfo = new UserPetInfo();
                userPetInfo.setUserId(user.getId());
                userPetInfo.setGender(user.getGender());
                userPetInfo.setNickname(user.getNickname());
                userPetInfo.setPhoneNumber(user.getPhoneNumber());
                userPetInfo.setPortrait(user.getPortrait());
                userPetInfo.setSignature(user.getSignature());
                Intent intent = new Intent(context, UserInfoActivity.class);
                intent.putExtra("user",userPetInfo);
                context.startActivity(intent);
            }
        });
        holder.btnFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = holder.btnFollow.getText().toString();
                if (s.equals("已关注")){
                    new AlertDialog.Builder(context)
                            .setTitle("温馨提示")
                            .setMessage("您确定要取消关注吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //连接服务器,传送宠物id
                                    userService.unFollowSomeone(new Follow(0, MyApplication.user.getId(), user.getId(), null)).enqueue(new Callback<ResponseBody<Object>>() {
                                        @Override
                                        public void onResponse(Call<ResponseBody<Object>> call, Response<ResponseBody<Object>> response) {
                                            ResponseBody<Object> body = response.body();
                                            if (body!=null){
                                                int code = body.getCode();
                                                if (code==200){
                                                    holder.btnFollow.setText("关注");
                                                }else if (code==500){
                                                    UIUtils.toast("服务器发生错误",false);
                                                }
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<ResponseBody<Object>> call, Throwable t) {
                                            UIUtils.toast("连接服务器失败",false);
                                        }
                                    });

                                }
                            })
                            .setNegativeButton("点错了", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            })
                            .setCancelable(false).show();
                }else if (s.equals("关注")){
                    userService.followSomeone(new Follow(0, MyApplication.user.getId(), user.getId(), new Date(System.currentTimeMillis()))).enqueue(new Callback<ResponseBody<Object>>() {
                        @Override
                        public void onResponse(Call<ResponseBody<Object>> call, Response<ResponseBody<Object>> response) {
                            ResponseBody<Object> body = response.body();
                            if (body!=null){
                                int code = body.getCode();
                                if (code==200){
                                    holder.btnFollow.setText("已关注");
                                }else if (code==500){
                                    UIUtils.toast("服务器发生错误",false);
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody<Object>> call, Throwable t) {
                            UIUtils.toast("连接服务器失败",false);
                        }
                    });
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout layoutEnter;
        private CircleImageView portrait;
        private ImageView ivSex;
        private TextView tvNickname;
        private TextView tvSignature;
        private Button btnFollow;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);
            portrait=itemView.findViewById(R.id.portrait);
            ivSex=itemView.findViewById(R.id.ivSex);
            tvNickname=itemView.findViewById(R.id.tvNickname);
            tvSignature=itemView.findViewById(R.id.tvSignature);
            btnFollow=itemView.findViewById(R.id.btnFollow);
        }
    }
}
